local ls = require("luasnip")
local s = ls.snippet
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node
local fmt = require("luasnip.extras.fmt").fmt

ls.add_snippets("javascript", {
  -- 箭头函数
  s("af", fmt("const {} = ({}) => {{\n  {}\n}}", {
    i(1, "functionName"),
    i(2, "params"),
    i(3, "// code"),
  })),

  s("sf", fmt("() => {{\n  {}\n}}", {
    i(1, ""),
  })),

  s("f", fmt("function {}({}) {{\n  {}\n}}", {
    i(1, "functionName"),
    i(2, "params"),
    i(3, "// code"),
  })),

  -- 异步函数
  s("asf", fmt("async function {}({}) {{\n  {}\n}}", {
    i(1, "functionName"),
    i(2, "params"),
    i(3, "// code"),
  })),

  -- try-catch
  s("tc", fmt([[
try {{
  {}
}} catch (error) {{
  {}
}}]], {
    i(1, "// code"),
    i(2, "console.error(error)"),
  })),

  -- 类
  s("cla", fmt([[
class {} {{
  constructor({}) {{
    {}
  }}

  {}
}}]], {
    i(1, "ClassName"),
    i(2, "params"),
    i(3, "// constructor"),
    i(4, "// methods"),
  })),

  -- Promise
  s("prom", fmt([[
new Promise((resolve, reject) => {{
  {}
}})]], {
    i(1, "// code"),
  })),

  -- async/await try-catch
  s("tca", fmt([[
try {{
  const {} = await {}
  {}
}} catch (error) {{
  {}
}}]], {
    i(1, "result"),
    i(2, "promise"),
    i(3, "// code"),
    i(4, "console.error(error)"),
  })),

  -- destructuring
  s("des", fmt("const {{ {} }} = {}", {
    i(1, "props"),
    i(2, "object"),
  })),

  -- array destructuring
  s("dar", fmt("const [ {} ] = {}", {
    i(1, "elements"),
    i(2, "array"),
  })),

  -- forEach
  s("fe", fmt("{}.forEach({} => {{\n  {}\n}})", {
    i(1, "array"),
    i(2, "item"),
    i(3, "// code"),
  })),

  --  if 
  s("if", fmt([[
  if ({}) {{
    {}
  }}]], {
    i(1, "condition"),
    i(2)
  })),

  -- map
  s("map", fmt("{}.map({} => {{\n  {}\n}})", {
    i(1, "array"),
    i(2, "item"),
    i(3, "return item"),
  })),

  -- filter
  s("fil", fmt("{}.filter({} => {{\n  {}\n}})", {
    i(1, "array"),
    i(2, "item"),
    i(3, "return condition"),
  })),

  -- reduce
  s("red", fmt("{}.reduce(({}, {}) => {{\n  {}\n}}, {})", {
    i(1, "array"),
    i(2, "acc"),
    i(3, "curr"),
    i(4, "return acc"),
    i(5, "initialValue"),
  })),

  -- module.exports
  s("exp", fmt("module.exports = {}", {
    i(1, ""),
  })),

  -- console.log
  s("st", fmt("console.log({});", { i(0) })),

  -- import from
  s("im", fmt([[import {{ {} }} from "{}";]], { i(2), i(1) })),

  -- vuex options
  s("vuexOptions", fmt([[{} = {{
  namespaced: true,
  actions: {{}},
  mutations: {{}},
  getters: {{}},
  state: {{}},
}};]], { i(0) })),

  -- eslint comment
  s("esc", t("// eslint-disable-next-line no-unused-vars")),

  -- require
  s("cq", fmt([[const {{ {} }} = require("{}")]], { i(2), i(1) })),

  -- main function
  s("main", fmt([[
(function () {{
  {}
}})()]], { i(0) })),
})

-- 同样适用于 typescript
ls.filetype_extend("typescript", { "javascript" })
ls.filetype_extend("javascriptreact", { "javascript" })
ls.filetype_extend("typescriptreact", { "javascript" })
